package tgc.edu.exam.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.exam.custom.DataGridParam;
import tgc.edu.exam.custom.DataGridUtils;
import tgc.edu.exam.custom.LayUiResult;
import tgc.edu.exam.entity.Permission;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.PermissionService;
import tgc.edu.exam.web.form.PermissionForm;
import tgc.edu.exam.web.form.StudentForm;

@Controller
@RequestMapping(value = "/permission")
public class PermissionController {
	@Autowired
	private PermissionService service;
	@Autowired
	private UserUtils userUtils;

	@RequestMapping(value = "/manage")
	public void manage() {
	}

	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(String id) {
		service.deleteById(UUID.fromString(id));
		return LayUiResult.getResult("删除成功");
	}

	@RequestMapping(value = "/search")
	public void search(PermissionForm form, ModelMap map) {
		map.put("model", form);
	}

	@RequestMapping(value = "/edit")
	public void edit(String id, ModelMap map) {
		Permission model = new Permission();
		if (id != null) {
			model = service.findById(UUID.fromString(id));
		}
		map.put("model", model);
	}

	@RequestMapping(value = "/page")
	@ResponseBody
	public Object page(DataGridParam param, PermissionForm form) {
		Sort sort = Sort.by("createDate");
		Pageable pageable = param.getPageable(sort);
		Specification<Permission> spec = buildSpec(form);
		Page<Permission> page = service.findAll(spec, pageable);
		return DataGridUtils.buildResult(page);
	}

	public Specification<Permission> buildSpec(PermissionForm form) {
		Specification<Permission> spec = new Specification<Permission>() {

			@Override
			public Predicate toPredicate(Root<Permission> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String name = form.getName();
				if (StringUtils.hasText(name)) {
					rules.add(cb.like(root.get("name"), "%"+name+"%"));
				}
				String code = form.getCode();
				if (StringUtils.hasText(code)) {
					rules.add(cb.equal(root.get("code"), "%"+code+"%"));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};

		return spec;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public Object save(PermissionForm form) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String id = form.getId();
		Permission model = new Permission();
		if (StringUtils.hasText(id)) {
			model = service.findById(UUID.fromString(id));
			model.setUpdateDate(sdf.format(new Date()));
			model.setUpdateName(userUtils.getName());
		} else {
			model.setCreateDate(sdf.format(new Date()));
			model.setCreateName(userUtils.getName());
		}
		BeanUtils.copyProperties(form, model, "id");
		service.save(model);
		return LayUiResult.getResult();
	}

}
